package com.dream.testtubebaby_doctor;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.testtubebaby_doctor.data.user.User;
import com.dream.testtubebaby_doctor.data.user.UserDataManager;
import com.dream.testtubebaby_doctor.data.user.UserDataManager.UserDataLishenter;
import com.dream.testtubebaby_doctor.utils.SharePreferencesManager;
import com.dream.testtubebaby_doctor.utils.StatusBarCompat;
import com.dream.testtubebaby_doctor.utils.ToastUtil;

public class LoginActivity extends Activity {
	private SharePreferencesManager mSharePreferencesManager;

	public static final String GUIDE_FLAG = "guide_flag";
	public static final int REQUEST_LOGIN = 100;
	private EditText mNameEditText;
	private EditText mPasswordEditText;
	private ImageView watch_password_img;
	private TextView mforgetTextView;
	private Button mLoginButton;
	private long firstBackTime;
	private boolean guide_flag = false;
	private boolean watch_password_visible = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_login_view);
		StatusBarCompat.compat(this,
				getResources().getColor(R.color.status_bar_color));
		initCommonManager();
		initContentView();
		guide_flag = getIntent().getBooleanExtra(GUIDE_FLAG, false);
	}

	private void initCommonManager() {
		mSharePreferencesManager = new SharePreferencesManager(this);
	}

	private void initContentView() {
		mNameEditText = (EditText) findViewById(R.id.edittext_login_name);
		mPasswordEditText = (EditText) findViewById(R.id.edittext_login_password);
		mLoginButton = (Button) findViewById(R.id.button_login);
		mLoginButton.setOnClickListener(mListener);
		mforgetTextView = (TextView) findViewById(R.id.textview_login_forget_password);
		mforgetTextView.setOnClickListener(mListener);
		watch_password_img = (ImageView) findViewById(R.id.watch_password_img);
		watch_password_img.setOnClickListener(mListener);
	}

	OnClickListener mListener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button_login:
				login();
				break;
			case R.id.textview_login_forget_password:
				findPassword();
				break;
			case R.id.watch_password_img:
				if (watch_password_visible) {
					watch_password_img
							.setImageResource(R.drawable.article_watch_icon);
					mPasswordEditText
							.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD
									| InputType.TYPE_CLASS_TEXT);
					watch_password_visible = false;
				} else {
					watch_password_img
							.setImageResource(R.drawable.article_watch_press_icon);
					mPasswordEditText
							.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
									| InputType.TYPE_CLASS_TEXT);
					watch_password_visible = true;
				}
				mPasswordEditText.setSelection(mPasswordEditText.getText()
						.toString().length());
				break;
			}
		}
	};

	public void login() {
		String name = mNameEditText.getText().toString().trim();
		String password = mPasswordEditText.getText().toString().trim();

		if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
			if (name.equals("1234") && password.equals("1234")) {// test
				startMainActivity();
				return;
			}
			User user = new User.UserBuilder().name(name).password(password)
					.build();
			UserDataManager userDataManager = new UserDataManager(this);
			userDataManager.login(user, new UserDataLishenter() {
				@Override
				public void requestUserData(User user) {
					mSharePreferencesManager.writeUserInfo(user.getName(),
							user.getPassword());
					mSharePreferencesManager.writeUserId(user.getId());
					mSharePreferencesManager.writePhotoUrl(user.getPhoto_url());
					File file = new File(Environment.getExternalStorageDirectory()
							+ SelectPhotoActivity.USER_PHOTO_DIR
							+ SelectPhotoActivity.USER_PHOTO_FILE_NAME);
					if(file.exists()){
						file.delete();
					}
					if (guide_flag) {
						Intent intent = new Intent(LoginActivity.this,
								ModifyPasswordActivity.class);
						intent.putExtra(ModifyPasswordActivity.LOGIN_FLAG_KEY,
								true);
						startActivity(intent);
						finish();
					} else {
						startMainActivity();
					}
				}
			});
		} else {
			ToastUtil.toast(this, R.string.user_login_input_error);
		}

	}

	private void startMainActivity() {
		Intent intent = new Intent();
		intent.setClass(LoginActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	protected void findPassword() {
	}

	@Override
	public void finish() {
		super.finish();
	}

	@Override
	public void onBackPressed() {
		if (firstBackTime + 2000 > System.currentTimeMillis()) {
			super.onBackPressed();
		} else {
			Toast.makeText(getApplicationContext(),
					getResources().getString(R.string.back_finish_msg),
					Toast.LENGTH_SHORT).show();
		}
		firstBackTime = System.currentTimeMillis();
	}
}